package kr.co.cherryt.data.parser;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import kr.co.cherryt.data.unit.AdInfo;
import kr.co.cherryt.data.unit.UserInfo;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class AdXmlParser {

	private AdXmlParser() {}

	public static final int RSS_TYPE_UNKNOWN = 0;
	public static final int RSS_TYPE_CORPORATE = 1;
	public static final int RSS_TYPE_PERSONAL = 2;
	public static final int RSS_TYPE_SHARE = 3;

	public static final ArrayList<AdInfo> GetAdXMLData(int rssType, UserInfo userInfo, int mAdListPosition) {

		
		String rssUrl;
		switch (rssType) {
		case RSS_TYPE_CORPORATE:
			rssUrl = "http://cherryt.co.kr/ct/index.php?mid=ct_ad&act=rss&id=" + userInfo.getUserID() + "&seq=" + mAdListPosition;
			break;
		case RSS_TYPE_PERSONAL:
			rssUrl = "http://cherryt.co.kr/ct/index.php?mid=ct_personal_ad&act=rss&id=" + userInfo.getUserID() + "&seq=" + mAdListPosition;
			break;
		case RSS_TYPE_SHARE:
			rssUrl = "http://cherryt.co.kr/ct/ct_share/rss?&seq=" + mAdListPosition;
			break;
		default:
			throw new IllegalArgumentException("Unknown Type!!");
		}

		//Log.d("cherryt", rssUrl);
		
		ArrayList<AdInfo> adInfoArray = new ArrayList<AdInfo>();

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();

			URL url = new URL(rssUrl);
			InputStream is = url.openStream();
			xpp.setInput(is, "utf-8");

			int eventType = xpp.getEventType();

			AdInfo adInfo = null;

			// XML 데이터 읽기
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {

					//Log.d("cherryt", "name: " + xpp.getName() );
					
					if (xpp.getName().equals("title")) {
						String text = xpp.nextText().trim();
						adInfo = new AdInfo();
						mAdListPosition++;
						
						adInfoArray.add(adInfo);
						adInfo.setTitle(text);
//						Log.d("cherryt", "title:" + text);
					} else if (xpp.getName().equals("writer")) {
						String text = xpp.nextText().trim();
						adInfo.setWriter(text);
//						Log.d("cherryt", "writer:" + text);
					} else if (xpp.getName().equals("image")) {
						String text = xpp.nextText().trim();
						adInfo.setImage(text);
//						Log.d("cherryt", "image:" + text);
					} else if (xpp.getName().equals("point")) {
						String text = xpp.nextText().trim();
						adInfo.setPoint(text);
//						Log.d("cherryt", "point:" + text);
					} else if (xpp.getName().equals("ppoint")) {
						String text = xpp.nextText().trim();
						adInfo.setPoint(text);
//						Log.d("cherryt", "ppoint:" + text);
					} else if (xpp.getName().equals("link")) {
						String text = xpp.nextText().trim();
						adInfo.setLink(text);
//						Log.d("cherryt", "link:" + text);
					} else if (xpp.getName().equals("time")) {
						String text = xpp.nextText().trim();
						adInfo.setTime(text);
//						Log.d("cherryt", "time:" + text);
					} else if (xpp.getName().equals("ad_cate")) {
						String text = xpp.nextText().trim();
						adInfo.setAdCate(text);
//						Log.d("cherryt", "ad_cate:" + text);
					} else if (xpp.getName().equals("sub_con")) {
						String text = xpp.nextText().trim();
						adInfo.setSubCon(text);
//						Log.d("cherryt", "sub_con:" + text);
					} else if (xpp.getName().equals("loc")) { 
						String text = xpp.nextText().trim();
						adInfo.setLoc(text);
//						Log.d("cherryt", "loc:" + text);
					}
				}

				eventType = xpp.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return adInfoArray;
	}
}
